using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class NewMainMenu_MusicSubMenu : MonoBehaviour
{
	public List<TextMeshProUGUI> BackgroundTextLines = new List<TextMeshProUGUI>();

	public TextMeshProUGUI MainText;

	[SerializeField]
	private List<int> BackgroundTextMaxCount = new List<int>();

	private int CurrentSelected;

	[SerializeField]
	private List<Hypothesis> PlayableHypotheticals = new List<Hypothesis>();

	[SerializeField]
	private GameObject ConsoleMenu;

	public Sprite noIconSprite;

	[SerializeField]
	private TextMeshProUGUI HypotheticalName;

	[SerializeField]
	private TextMeshProUGUI HypotheticalComposers;

	[SerializeField]
	private TextMeshProUGUI HypotheticalTagline;

	[SerializeField]
	private Image HypotheticalIcon;

	[SerializeField]
	private Image Track_Playtime;

	[SerializeField]
	private Image Track_PlaytimeBackground;

	[SerializeField]
	private AudioSource[] BackgroundNoiseToDisable;

	private bool AllTextShown;

	public bool AllowInput;

	private bool viewingTracks;

	private Hypothesis currentHypothesis;

	private List<HypotheticalMusicTrack> currentTracks = new List<HypotheticalMusicTrack>();

	private bool wasEnabled;

	private void Awake()
	{
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			BackgroundTextMaxCount.Add(backgroundTextLine.text.Length);
			backgroundTextLine.maxVisibleCharacters = 0;
		}
	}

	private void Update()
	{
		if (!GonerMenu.Instance.GonerMenuOpen && AllowInput)
		{
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Down))
			{
				CurrentSelected++;
				ClampCursor();
				UpdateDisplay(highlight: true);
				MusicManager.StopSong(Fade: false, 0f);
				ToggleBackgroundNoise(Pause: false);
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Up))
			{
				CurrentSelected--;
				ClampCursor();
				UpdateDisplay(highlight: true);
				MusicManager.StopSong(Fade: false, 0f);
				ToggleBackgroundNoise(Pause: false);
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) || Input.GetKeyDown(PlayerInput.Instance.Key_Menu))
			{
				SelectItem();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel))
			{
				if (viewingTracks)
				{
					NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
					viewingTracks = false;
					currentHypothesis = null;
					CurrentSelected = 0;
					UpdateDisplay(highlight: true);
					MusicManager.StopSong(Fade: false, 0f);
				}
				else
				{
					NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
					ConsoleMenu.SetActive(value: true);
					base.gameObject.SetActive(value: false);
					MusicManager.StopSong(Fade: false, 0f);
				}
				ToggleBackgroundNoise(Pause: false);
			}
		}
		if (MusicManager.Instance.source.clip != null)
		{
			Track_Playtime.fillAmount = MusicManager.Instance.source.time / MusicManager.Instance.source.clip.length;
			if (Track_Playtime.fillAmount != 0f)
			{
				Track_PlaytimeBackground.fillAmount = 1f;
			}
			else
			{
				Track_PlaytimeBackground.fillAmount = 0f;
			}
		}
		else
		{
			Track_Playtime.fillAmount = 0f;
			Track_PlaytimeBackground.fillAmount = 0f;
		}
		if (!wasEnabled && base.gameObject.activeSelf)
		{
			wasEnabled = true;
			AllowInput = true;
			CurrentSelected = 0;
			ToggleBackgroundNoise(Pause: false);
			StartCoroutine(ShowBackgroundText());
			UpdateDisplay(highlight: true);
		}
	}

	private void ToggleBackgroundNoise(bool Pause)
	{
		AudioSource[] backgroundNoiseToDisable = BackgroundNoiseToDisable;
		foreach (AudioSource audioSource in backgroundNoiseToDisable)
		{
			if (Pause)
			{
				audioSource.Pause();
			}
			else
			{
				audioSource.UnPause();
			}
			CameraManager.instance.ReverbFilter.enabled = !Pause;
		}
	}

	private void ClampCursor()
	{
		int num = (viewingTracks ? (currentTracks.Count + 1) : PlayableHypotheticals.Count);
		if (CurrentSelected >= num)
		{
			CurrentSelected = 0;
		}
		if (CurrentSelected < 0)
		{
			CurrentSelected = num - 1;
		}
		NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuMove);
	}

	private void SelectItem()
	{
		if (viewingTracks)
		{
			if (CurrentSelected == 0)
			{
				viewingTracks = false;
				currentHypothesis = null;
				CurrentSelected = 0;
				UpdateDisplay(highlight: true);
				MusicManager.StopSong(Fade: false, 0f);
				ToggleBackgroundNoise(Pause: false);
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
			}
			else
			{
				HypotheticalMusicTrack hypotheticalMusicTrack = currentTracks[CurrentSelected - 1];
				if (hypotheticalMusicTrack != null && hypotheticalMusicTrack.TrackFile != null)
				{
					MusicManager.PlaySong(hypotheticalMusicTrack.TrackFile, FadePreviousSong: false, 0f);
					ToggleBackgroundNoise(Pause: true);
				}
			}
		}
		else
		{
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
			if (PlayableHypotheticals[CurrentSelected] == null)
			{
				ConsoleMenu.SetActive(value: true);
				base.gameObject.SetActive(value: false);
				return;
			}
			currentHypothesis = PlayableHypotheticals[CurrentSelected];
			currentTracks = new List<HypotheticalMusicTrack>(currentHypothesis.HypotheticalMusicTracks);
			viewingTracks = true;
			CurrentSelected = 0;
			UpdateDisplay(highlight: true);
		}
	}

	private void UpdateDisplay(bool highlight)
	{
		string text = "";
		if (viewingTracks)
		{
			text += "<color=yellow><size=42>选择音乐</size></color>\n\n";
			for (int i = 0; i <= currentTracks.Count; i++)
			{
				if (i == 0)
				{
					text += ((CurrentSelected == 0) ? "<color=yellow>-> ...返回</color>\n" : "...返回\n");
					if (CurrentSelected == 0)
					{
						HypotheticalName.text = "";
						HypotheticalComposers.text = "";
						HypotheticalTagline.text = "";
						HypotheticalIcon.sprite = noIconSprite;
						HypotheticalIcon.color = new Color(1f, 1f, 1f, 0f);
					}
					continue;
				}
				HypotheticalMusicTrack hypotheticalMusicTrack = currentTracks[i - 1];
				string text2 = ((CurrentSelected == i) ? "<color=yellow>-> " : "");
				text2 += hypotheticalMusicTrack.TrackName;
				text2 += ((CurrentSelected == i) ? "</color>\n" : "\n");
				text += text2;
				if (CurrentSelected == i)
				{
					HypotheticalName.text = hypotheticalMusicTrack.TrackName;
					HypotheticalComposers.text = hypotheticalMusicTrack.Composer;
					HypotheticalTagline.text = hypotheticalMusicTrack.TrackDescription;
					HypotheticalIcon.sprite = currentHypothesis.HypothesisMenuSprite ?? noIconSprite;
					HypotheticalIcon.color = new Color(1f, 1f, 1f, 1f);
				}
			}
		}
		else
		{
			text += "<color=yellow><size=42>选择猜想</size></color>\n\n";
			for (int j = 0; j < PlayableHypotheticals.Count; j++)
			{
				Hypothesis hypothesis = PlayableHypotheticals[j];
				if (hypothesis == null)
				{
					text += ((CurrentSelected == j) ? "<color=yellow>-> ...返回</color>\n" : "...返回\n");
					continue;
				}
				MonoBehaviour.print("Current PlayableHypothetical = " + j);
				MonoBehaviour.print("Hypothetical = " + hypothesis);
				MonoBehaviour.print("Hypothetical Name = " + hypothesis.HypothesisName);
				string text3 = ((CurrentSelected == j) ? "<color=yellow>-> " : "");
				text3 = ((!(hypothesis != null) || hypothesis.HypotheticalMusicTracks == null || hypothesis.HypotheticalMusicTracks.Length == 0) ? (text3 + hypothesis.HypothesisName + " (0)") : (text3 + hypothesis.HypothesisName + $" ({hypothesis.HypotheticalMusicTracks.Length})"));
				text3 += ((CurrentSelected == j) ? "</color>\n" : "\n");
				text += text3;
				if (CurrentSelected == j)
				{
					HypotheticalName.text = hypothesis.HypothesisName;
					HypotheticalComposers.text = "";
					HypotheticalTagline.text = hypothesis.HypothesisTagline;
					HypotheticalIcon.sprite = hypothesis.HypothesisMenuSprite ?? noIconSprite;
					HypotheticalIcon.color = new Color(1f, 1f, 1f, 1f);
				}
			}
		}
		ChangeMainText(text, animated: true);
	}

	private void ChangeMainText(string text, bool animated)
	{
		MainText.text = text;
	}

	private IEnumerator ShowBackgroundText()
	{
		if (SettingsManager.Instance.GetBoolSettingValue("SimpleVFX"))
		{
			yield break;
		}
		List<TextMeshProUGUI> finishedTexts = new List<TextMeshProUGUI>();
		while (!AllTextShown)
		{
			yield return null;
			foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
			{
				if (!finishedTexts.Contains(backgroundTextLine) && backgroundTextLine.maxVisibleCharacters >= backgroundTextLine.text.Length)
				{
					finishedTexts.Add(backgroundTextLine);
				}
				else if (backgroundTextLine.maxVisibleCharacters < backgroundTextLine.text.Length)
				{
					float num = 100 + Random.Range(-60, -10);
					backgroundTextLine.maxVisibleCharacters += Mathf.CeilToInt(num * Time.deltaTime);
					backgroundTextLine.maxVisibleCharacters = Mathf.Clamp(backgroundTextLine.maxVisibleCharacters, 0, backgroundTextLine.text.Length);
					NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
				}
			}
		}
	}
}
